﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Inaction.UI.Common
{
    public class BindingHelper
    {
        internal static void SetBindingControlReadOnly(bool readOnly, BindingSource bindingSource)
        {
            foreach (Binding binding in bindingSource.CurrencyManager.Bindings)
            {
                if (binding.Control is BaseEdit)
                {
                    var control = binding.Control as BaseEdit;
                    control.Properties.ReadOnly = readOnly;
                }
            }
        }

        public static System.Type GetListItemType(BindingSource bindingSource)
        {
            if (bindingSource == null || bindingSource.DataSource == null) return null;
            if (string.IsNullOrEmpty(bindingSource.DataMember))
            {
                return ListBindingHelper.GetListItemType(bindingSource.DataSource);
            }
            else
            {
                var masterBindingSource = bindingSource.DataSource as BindingSource;
                var masterType = GetListType(masterBindingSource);
                var type = ListBindingHelper.GetListItemType(masterType, bindingSource.DataMember);
                return type;
            }
        }
        public static System.Type GetListType(BindingSource bindingSource, Type masterType = null)
        {
            if (bindingSource == null || bindingSource.DataSource == null) return null;
            if (string.IsNullOrEmpty(bindingSource.DataMember))
            {
                var type = ListBindingHelper.GetList(bindingSource.DataSource);
                return (type is Type) ? (Type)type : type.GetType();
            }
            else if (masterType != null)
            {
                Type type = ListBindingHelper.GetListItemType((object)masterType);
                return (type as System.Type).GetProperty(bindingSource.DataMember).PropertyType;
            }
            else
            {
                var masterBindingSource = bindingSource.DataSource as BindingSource;
                var type = GetListType(masterBindingSource);
                if (type == null) return null;
                return GetListType(bindingSource, type);
            }
        }

        private static Dictionary<Type, string> GetTypePath(Dictionary<Type, string> dictionary, BindingSource bindingSource)
        {
            if (string.IsNullOrEmpty(bindingSource.DataMember))
            {
                dictionary.Add(ListBindingHelper.GetListItemType(bindingSource), string.Empty);
            }
            else
            {
                GetTypePath(dictionary, bindingSource.DataSource as BindingSource);
                var type = ListBindingHelper.GetListItemType(dictionary.First().Key, bindingSource.DataMember);
                dictionary.Add(type, bindingSource.DataMember);
            }
            return dictionary;
        }
    }
}
